<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: awfigq(67877605@qq.com)
// +----------------------------------------------------------------------

// TrioMobile
include_once("Sms.class.php");
include_once(ROOT_PATH."ThinkPHP/Vendor/Sms/KetnoiSMS/sendsms.php");

class KetnoiSms implements Sms
{
	public $config = array(
	);	
	
	public $sms;
	public $message = "";
	public $smsInfo;
   
  public function __construct($smsInfo = '')
  { 	
		if(!empty($smsInfo))
		{
			set_time_limit(0);
			$this->smsInfo = $smsInfo;
		}
  }
	
	public function sendSMS($mobiles=array(),$content,$sendTime='')
	{
		//$mobileLen = 90;
		//$messageLen = 1000;
		
		$contentLen = mb_strlen($content);
		//$smsTotalCount = ceil($contentLen / $messageLen) * count($mobiles);
		
		//$mobileList = array_chunk($mobiles,$mobileLen);
		$successNum = 0;
		$smsTotalCount = count($mobiles);
		foreach($mobiles as $mobileItem)
		{
			$smsCount = 1;
			$result = doSendMTSpam($mobileItem,$content, $this->smsInfo['user_name'], $this->smsInfo['password']);
			
			$smsLog['send_content'] = addslashes($content);
			$smsLog['send_mobiles'] = $mobileItem;
			
			$smsLog['success_mobiles'] = "";
			$smsLog['fail_mobiles'] = "";

			if($result['status'])
			{
				$smsLog['success_count'] = $result['success'];
				$smsLog['expense_count'] = ceil($contentLen / 160) * $result['success'];
				$smsLog['fail_count'] = $smsCount - $result['success'];
				$smsLog['action_message'] = $result['msg'];
				$successNum += $smsCount;
			}
			else
			{
				$smsLog['success_count'] = 0;
				$smsLog['fail_count'] = $smsCount;
				$smsLog['expense_count'] = 0;
				$smsLog['action_message'] = $result['msg'];
			}
			
			$smsLog['send_time'] = a_gmtTime();
			
			if(intval(a_fanweC('SMS_SEND_LOG')) == 1)
			{
				$sql = "insert into ".$GLOBALS['db_config']['DB_PREFIX']."sms_send_log (class_name,send_content,success_count,success_mobiles,fail_mobiles,expense_count,fail_count,action_message,send_time) values('TrioMobile','".$smsLog['send_content']."','".$smsLog['success_count']."','".$smsLog['success_mobiles']."','".$smsLog['fail_mobiles']."','".$smsLog['expense_count']."','".$smsLog['fail_count']."','".$smsLog['action_message']."','".$smsLog['send_time']."')";
				
				$GLOBALS['db']->query($sql);
			}
		}
		
		if($smsTotalCount > $successNum)
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}
}
?>